package com.anlu.base.common.feign;

import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
import com.anlu.base.common.feign.core.PigFeignInnerRequestInterceptor;
import com.anlu.base.common.feign.core.PigFeignRequestCloseInterceptor;
import com.anlu.base.common.feign.sentinel.SentinelAutoConfiguration;
import com.anlu.base.common.feign.sentinel.ext.PigSentinelFeign;
import feign.Feign;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.openfeign.PigFeignClientsRegistrar;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;

@Configuration(proxyBeanMethods = false)
@Import(PigFeignClientsRegistrar.class)
@AutoConfigureBefore(SentinelFeignAutoConfiguration.class)
public class PigFeignAutoConfiguration {

    @Bean
    @Scope("prototype")
    @ConditionalOnMissingBean
    @ConditionalOnProperty(name = "feign.sentinel.enabled")
    public Feign.Builder feignSentinelBuilder() {
        return PigSentinelFeign.builder();
    }


    @Bean
    public PigFeignRequestCloseInterceptor pigFeignRequestCloseInterceptor(){
        return new PigFeignRequestCloseInterceptor();
    }

    /**
     * add inner request header
     * @return PigFeignInnerRequestInterceptor
     */
    @Bean
    public PigFeignInnerRequestInterceptor pigFeignInnerRequestInterceptor() {
        return new PigFeignInnerRequestInterceptor();
    }
}
